package com.htudata.zabbix.api.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TriggerZabbixApiService {

  @Autowired
  private BaseZabbixApiService baseZabbixApiService;

  public String createTriggers(JSONArray triggers) throws Exception {
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "trigger.create");
    request.put("params", triggers);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  public String updateTriggers(JSONArray triggers) throws Exception {
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "trigger.update");
    request.put("params", triggers);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  public String deleteTriggers(JSONArray triggerIds) throws Exception {
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "trigger.delete");
    request.put("params", triggerIds);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  public String getTriggers(JSONObject params) throws Exception {
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "trigger.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONResult(request);
  }
  
  @Deprecated
  private JSONArray getTriggerInfoByHostId(String[] output, String[] hostids) {
    JSONObject params = new JSONObject();
    params.put("output", output);
    params.put("hostids", hostids);
    params.put("selectHosts", new String[] { "hostid", "host" });
    params.put("selectItems", new String[] { "itemid", "key_", "hostid" });
    params.put("selectFunctions", new String[] { "functionid", "name", "parameter", "itemid" });
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "trigger.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONArrayResult(request);
  }
  
  @Deprecated
  private JSONArray getTriggerInfoByItemId(String[] output, String[] itemids) {
    JSONObject params = new JSONObject();
    params.put("output", output);
    params.put("itemids", itemids);
    JSONObject request = new JSONObject();
    request.put("jsonrpc", "2.0");
    request.put("method", "trigger.get");
    request.put("params", params);
    request.put("id", Integer.valueOf(0));
    return this.baseZabbixApiService.getJSONArrayResult(request);
  }
}
